﻿using Book_WebAPI.Data;
using Book_WebAPI.Models;
using Book_WebAPI.Services;
using Microsoft.AspNetCore.Mvc;

namespace Book_WebAPI.Controllers
{
    [Route("api/authors")]
    [ApiController]
    public class AuthorController : ControllerBase
    {
        // 依赖注入-仓储接口
        public IAuthorRepository AuthorRepository { get; }
        public AuthorController(IAuthorRepository authorRepository)
        {
            AuthorRepository = authorRepository;
        }

        /// <summary>
        /// 获取作者详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<Author>> GetAuthors()
        {
            return AuthorRepository.GetAuthors().ToList();
        }

        /// <summary>
        /// 根据id获取指定作者
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Author> GetAuthor(int id)
        {
            var author = AuthorRepository.GetAuthor(id);
            if (author == null)
            {
                return NotFound();
            }
            else
            {
                return author;
            }
        }

        /// <summary>
        /// 新增作者
        /// </summary>
        /// <param name="authorCreate"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddAuthor(AuthorCreate authorCreate)
        {
            var author = new Author
            {
                id = authorCreate.id,
                name = authorCreate.name,
                age = authorCreate.age,
                email = authorCreate.email
            };
            AuthorRepository.AddAuthor(author);
            return CreatedAtRoute(nameof(AddAuthor), new { id = author.id }, author);
        }

        /// <summary>
        /// 根据id获取最新添加的作者
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("/GetNewAddAuthor/{id}", Name = nameof(AddAuthor))]
        public ActionResult<Author> GetNewAddAuthor(int id)
        {
            var author = AuthorRepository.GetAuthor(id);
            if (author == null)
            {
                return NotFound();
            }
            else
            {
                return author;
            }
        }

        /// <summary>
        /// 根据id删除指定作者，并删除该作者的所有图书
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteAuthor(int id)
        {
            var author = AuthorRepository.GetAuthor(id);
            if (author == null)
            {
                return NotFound();
            }
            AuthorRepository.DeleteAuthor(author);
            return Ok("删除成功");
        }

        /// <summary>
        /// 更新作者信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="authorUpdate"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult UpdateAuthor(int id, AuthorUpdate authorUpdate)
        {
            var author = AuthorRepository.GetAuthor(id);
            if (author == null)
            {
                return NotFound();
            }
            AuthorRepository.UpdateAuthor(id, authorUpdate);
            return Ok("更新成功");
        }
    }
}
